package com.jiang.controller;

import com.jiang.jopo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author jiangjingbo
 * @version V1.0
 * @Project java2203
 * @Package com.jiang.controller
 * @Description:
 * @Date 2022/6/7 19:32
 */
//将test1类交给Spring框架管理
    @Controller
    @RequestMapping("/parm")
public class test1 {
//方法如何接收参数(重点)
    @RequestMapping("/test1")
    public String test1(Integer id, String name , Model model){

        System.out.println("===========id===="+id);
        System.out.println("==========name======="+name);
        //将数据带到页面中
        model.addAttribute("id",id);
        model.addAttribute("name",name);
        return  "hello";
    }

    @RequestMapping("/test2")
    public String test2(User user, Model model){
        System.out.println("==========user======="+user);
        //将数据带到页面中
        model.addAttribute("user",user);
        System.out.println("====test====");
        return  "hello";
    }

    @RequestMapping("/test3")
    public String test3(String[] array ,Model model){
        System.out.println("=============array======"+array);
        model.addAttribute("array",array);
        return "hello";
    }
    @RequestMapping("/test4")
    public String test4(List<User> list, Model model ){
        System.out.println("============"+list);
        model.addAttribute("list",list);
        return "hello";
    }

    @RequestMapping("test5")
    public String test5(HttpServletRequest request , HttpServletResponse response ,Model model){
        String id = request.getParameter("id");
        String name = request.getParameter("name");

        model.addAttribute("id",id);
        model.addAttribute("name",name);
        System.out.println("=========="+id);
        System.out.println("=========="+name);
        return "hello";
    }
    @RequestMapping("/test6/{id}/{name}")
    public String test6(@PathVariable("id") Integer id , @PathVariable("name") String name , Model model){
        System.out.println("=====id======"+id);
        System.out.println("======name======="+name);
        Model id1 = model.addAttribute("id", id);
        model.addAttribute("name",name);


        return "hello";
    }

    /**
     * 请求转发
     * @param id
     * @param name
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/test7")
    public String test7(Integer id, String name , Model model, HttpServletRequest request){

//        model.addAttribute("id", id);
//        model.addAttribute("name", name);
        request.setAttribute("id",id);
        request.setAttribute("name",name);

        return "forward:/parm/test8";
    }
    @RequestMapping("/test8")
    public String test8(Integer id, String name, HttpServletRequest request,Model model){

        model.addAttribute("id",id);
        request.setAttribute("name",name);
        System.out.println(id+"============="+name);
        return "redirect:http://www.baidu.com";
    }

    /**
     * 测试错误
     */
    @RequestMapping("/test9")
    public String test9(){
        int i =1/0;
        return "hello";
    }
}
